package com.wisezone.java1902.d12_conections;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Student {
	private String name; // 姓名
	private int number; // 学号

	public Student(String name, int number) {
		this.name = name;
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	@Override
	public String toString() {
		return "学生 [姓名=" + name + ", 学号=" + number + "]";
	}

	@Override
	public boolean equals(Object obj) {
		Student stu = (Student) obj;
		return this.getName().equals(stu.getName()) && this.getNumber() == stu.getNumber() ? true : false;
	}

	@Override
	public int hashCode() {
		return this.getName().hashCode() * 37 + this.number;
	}
	
	public static void main(String[] args) {
		Set<Student> sets = new HashSet<>(); // 实例化一个set集合容器
		Student stu1 = new Student("张三",1001);
		Student stu2 = new Student("李四",1002);
		Student stu3 = new Student("王二",1003);
		Student stu4 = new Student("张三",1001);
		
		System.out.println("stu1 :"+stu1.hashCode());
		System.out.println("stu2 :"+stu2.hashCode());
		System.out.println("stu3 :"+stu3.hashCode());
		System.out.println("stu4 :"+stu4.hashCode());

		sets.add(stu1);
		sets.add(stu2);
		sets.add(stu3);
		sets.add(stu4);
		
//		Iterator<Student> iterator = sets.iterator();
//		while(iterator.hasNext()) {
//			System.out.println(iterator.next());
//		}
		// for-each
		for (Student student : sets) {
			System.out.println(student);
		}

	}

}
